/**
 * Carnegie Mellon University
 * School of Computer Science
 * MSIT eBusiness Technology
 * 
 * Team 8 - Infinity (2011 Spring)
 * infinity-team8@googlegroups.com
 * 
 * Professor: Jeffrey L. Eppinger
 * Program Faculty: Sujata Telang
 */

package controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.EmployeeDAO;
import model.Model;
import model.CustomerDAO;

import org.mybeans.dao.DAOException;
import org.mybeans.form.FormBeanException;
import org.mybeans.form.FormBeanFactory;

import databean.CustomerBean;
import databean.EmployeeBean;
import formbean.EmployeeResetCustomerPwdForm;


public class EmployeeResetCustomerPasswordAction extends Action {
	private FormBeanFactory<EmployeeResetCustomerPwdForm> formBeanFactory = FormBeanFactory.getInstance(EmployeeResetCustomerPwdForm.class);

	private CustomerDAO customerDAO;
	private EmployeeDAO employeeDAO;
	
	public EmployeeResetCustomerPasswordAction(Model model) {
		customerDAO = model.getCustomerDAO();
		employeeDAO = model.getEmployeeDAO();
	}

	public String getName() { return "employee-reset-customer-password.do"; }

    public String perform(HttpServletRequest request,HttpServletResponse response) {
        List<String> errors = new ArrayList<String>();
        request.setAttribute("errors",errors);
        
        //returns the existing session or null, if the session does not exist.
        HttpSession session = request.getSession(false);

        try {
        	

	        request.setAttribute("customerList",customerDAO.getCustomers());
	        request.setAttribute("employeeList",employeeDAO.getEmployees());
			request.setAttribute("employee",(EmployeeBean) request.getSession(false).getAttribute("employee"));
        	
        	EmployeeResetCustomerPwdForm form = formBeanFactory.create(request);
	        request.setAttribute("form",form);
	        
	        request.setAttribute("customerList",customerDAO.getCustomers());
	        request.setAttribute("employeeList",employeeDAO.getEmployees());
	        
	        // If no params were passed, return with no errors so that the form will be
	        // presented (we assume for the first time).
	        if (!form.isPresent()) {
	            return "employee-reset-customer-password.jsp";
	        }
	
	        // Any validation errors?
	        errors.addAll(form.getValidationErrors());
	        if (errors.size() != 0) {
	            return "employee-reset-customer-password.jsp";
	        }

	        CustomerBean customer = customerDAO.lookup(form.getUserName());
	        	        
			// Change the password
        	customerDAO.setPassword(customer.getUserName(),form.getNewPassword());
	
	        	
	        // After successful registration (and login) send to...
	        String redirectTo = (String) session.getAttribute("redirectTo");
	        if (redirectTo != null) return redirectTo;
	        
			request.setAttribute("message","Customer " + customer.getUserName() + "'s password has been reset successfully!");
	        return "employee-success.jsp";
        } catch (DAOException e) {
        	errors.add(e.getMessage());
        	return "employee-create-customer-account.jsp";
        } catch (FormBeanException e) {
        	errors.add(e.getMessage());
        	return "employee-create-customer-account.jsp";
        }
    }
}
